<?php 
/**
#	Project: PHPDISK File Storage Solution
#	This is NOT a freeware, use is subject to license terms.
#
#	Site: http://www.phpdisk.com
#
#	$Id: client_sub.php 4 2013-01-09 13:32:24Z along $
#
#	Copyright (C) 2008-2013 PHPDisk Team. All Rights Reserved.
#
*/
include "../../includes/commons.inc.php";
@set_time_limit(0);
$agent = $_SERVER['HTTP_USER_AGENT'];
if($agent!='phpdisk-client'){
	exit('<a href="http://faq.phpdisk.com/search?w=p403&err=code" target="_blank">[PHPDisk Access Deny] Invalid Entry!</a>');
}
// checked username and pwd...
$username = trim(gpc('username','GP',''));
$password = trim(gpc('password','GP',''));

$userinfo = $db->fetch_one_array("select userid from {$tpf}users where username='$username' and password='$password'");
if(!$userinfo){
	$str = '网盘登录出错：用户名或密码不正确，请重新输入';
	if(is_utf8()){
		echo convert_str('utf-8','gbk',$str);
	}else{
		echo $str;
	}
}else{
	$uid = (int)$userinfo[userid];
}

switch ($action){
	case 'resume':
		echo $action.LF;
		$q = $db->query("select * from {$tpf}uploadx_files where file_state=0 and userid='$uid'");
		while ($rs = $db->fetch_array($q)) {
			$tmp_ext = $rs[file_extension] ? '.'.$rs[file_extension] : '';
			$dir1 = PHPDISK_ROOT.$settings[file_path].'/@'.$uid.'/';
			$dir2 = PHPDISK_ROOT.$settings[file_path].'/'.$rs[file_store_path];
			//make_dir($dir1);
			//make_dir($dir2);
			$file = $dir1.$rs[file_real_name].$tmp_ext;
			$file_dest = $dir2.$rs[file_real_name].$tmp_ext;
			if(@filesize($file)==(int)$rs[file_size]){
				//rename($file,$file_dest);
				$db->query_unbuffered("update {$tpf}uploadx_files set file_state=1 where id='{$rs[id]}'");
			}else{
				$str = $rs[file_name].$tmp_ext.'|'.$rs[file_size].'|'.$rs[file_local_path];
				if(is_utf8()){
					echo convert_str('utf-8','gbk',$str);
				}
				echo $str.LF;
			}
		}
		$db->free($q);
		unset($rs);
		break;

	case 'update':
		if($settings['open_multi_server']){
			$server_oid = @$db->result_first("select server_oid from {$tpf}servers where server_id>1 order by is_default desc limit 1");
		}
		$q = $db->query("select * from {$tpf}uploadx_files where file_state=0 and userid='$uid'");
		while ($rs = $db->fetch_array($q)) {
			$tmp_ext = $rs[file_extension] ? '.'.$rs[file_extension] : '';
			$dir1 = PHPDISK_ROOT.$settings[file_path].'/@'.$uid.'/';
			$dir2 = PHPDISK_ROOT.$settings[file_path].'/'.$rs[file_store_path];
			//make_dir($dir1);
			//make_dir($dir2);
			$file = $dir1.$rs[file_real_name].$tmp_ext;
			$file_dest = $dir2.$rs[file_real_name].get_real_ext($rs[file_extension]);
			if(@filesize($file)==(int)$rs[file_size]){
				if(file_exists($file_dest)){
					@unlink($file);
				}else{
					@rename($file,$file_dest);
				}
				//$db->query_unbuffered("update {$tpf}uploadx_files set file_state=1 where id='{$rs[id]}'");
				$ins = array(
				'file_name' => $rs[file_name],
				'file_key' => random(8),
				'file_extension' => $rs[file_extension],
				//'is_image' => $is_image,
				'file_mime' => 'application/octet-stream',
				'file_description' => '',
				'file_store_path' => $rs[file_store_path],
				'file_real_name' => $rs[file_real_name],
				'file_md5' => '',
				'server_oid' => (int)$server_oid,
				'file_size' => $rs[file_size],
				'file_time' => $timestamp,
				'is_checked' => 1,
				'in_share' => 0,
				'userid' => $uid,
				'folder_id'=>$rs[folder_id],
				'ip' => $onlineip,
				);
				$db->query_unbuffered("insert into {$tpf}files set ".$db->sql_array($ins));

				$db->query_unbuffered("delete from {$tpf}uploadx_files where id='{$rs[id]}'");
			}

		}
		$db->free($q);
		unset($rs);
		echo $action.LF;
		break;
	case 'postlist':
		$fl = trim(gpc('fl','P','',0));
		$folder_id = (int)gpc('folder_id','P',0);
		$dir_name = date('Y/m/d/');
		$dir = PHPDISK_ROOT.$settings[file_path].'/'.$dir_name;
		make_dir($dir);
		make_dir(PHPDISK_ROOT.$settings[file_path].'/@'.$uid.'/');

		$arr = explode('|',convert_str('gbk','utf-8',$fl));
		//write_file(PHPDISK_ROOT.'system/b.txt',$fl,'ab');
		for($i=0;$i<count($arr)-1;$i++){
			$arr2 = explode('>',$arr[$i]);
			$file_extension = get_extension($arr2[2]);
			$esp = strlen($file_extension)+1;
			$file_name = $file_extension ? substr($arr2[2],0,strlen($arr2[2])-$esp) : $arr2[2];
			$arr3 = explode('.',$arr2[0]);
			$num = (int)@$db->result_first("select count(*) from {$tpf}uploadx_files where userid='$uid' and file_store_path='$dir_name' and file_real_name='{$arr3[0]}'");
			if(!$num){
				$ins = array(
				'userid'=>$uid,
				'file_name'=>$file_name,
				'file_extension'=>$file_extension,
				'file_size'=>$arr2[1],
				'file_local_path'=>$db->escape(str_replace('\\\\','\\',$arr2[3])),
				'file_store_path'=>$dir_name,
				'file_real_name'=>$arr3[0],
				'file_time'=>$timestamp,
				'folder_id'=>$folder_id ? $folder_id : '-1',
				'ip'=>$onlineip,
				);
				//write_file(PHPDISK_ROOT.'system/a.txt',var_export($ins,true),'ab');
				$db->query_unbuffered("insert into {$tpf}uploadx_files set ".$db->sql_array($ins)."");
			}
		}
		echo $action;
		break;

	default:
		exit('<a href="http://faq.phpdisk.com/search?w=p404&err=code" target="_blank">[PHPDisk Access Deny] Connect Error!</a>');
}
?>